package com.andatsoft.laisim.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;

import com.andatsoft.laisim.entity.WPPoint;
import com.andatsoft.laisim.theme.ThemeManager;

public class WPProgressBar extends View {

	private final float DELTA_TIME = 1 / 15f;
	private int mPointColor = 0xffffffff;
	private int mBackgroundColor = 0x00000000;
	private int mLayoutWidth;
	private int mLayoutHeight;

	private WPPoint[] mPoint = new WPPoint[5];

	private Paint mBackgroundPainter;
	private Paint mPointPainter;

	private Handler mHandler = new Handler();

	public WPProgressBar(Context context) {
		super(context);
		applySkin();
		initPoints();
	}

	public WPProgressBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		applySkin();
		initPoints();
	}

	public void applySkin() {
		if (ThemeManager.getSelectedTheme() != null) {
			mPointColor = ThemeManager.getSelectedTheme().getNormalTextColor();
			mBackgroundColor = ThemeManager.getSelectedTheme()
					.getDialogBackgroundColor();
		}
		if (mBackgroundPainter == null)
			mBackgroundPainter = new Paint();
		mBackgroundPainter.setColor(mBackgroundColor);
		mBackgroundPainter.setAntiAlias(true);
		mBackgroundPainter.setStyle(Style.FILL);
		mBackgroundPainter.setAlpha(0);

		if (mPointPainter == null)
			mPointPainter = new Paint();
		mPointPainter.setColor(mPointColor);
		mPointPainter.setAntiAlias(true);
	}

	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mLayoutWidth = w;
		mLayoutHeight = h;
	}

	private Runnable mUpdatePoint = new Runnable() {
		public void run() {
			for (int i = 0; i < mPoint.length; i++) {
				calAcceleration(mPoint[i]);
				mPoint[i].x += 79f * DELTA_TIME + mPoint[i].acceleration
						* (DELTA_TIME * DELTA_TIME) / 2;
			}
			if (mPoint[4].x > mLayoutWidth)
				resetPoint();
			invalidate();

		}
	};

	private void initPoints() {
		mPoint[0] = new WPPoint(-5f);
		mPoint[1] = new WPPoint(-55f);
		mPoint[2] = new WPPoint(-105f);
		mPoint[3] = new WPPoint(-155f);
		mPoint[4] = new WPPoint(-205f);
	}

	private void calAcceleration(WPPoint p) {
		if (p.x < mLayoutWidth * 4 / 10f) {
			p.acceleration = 1000f;
		} else if (p.x < mLayoutWidth * 6 / 10f) {
			p.acceleration = -1500f;
		} else if (p.x < mLayoutWidth) {
			p.acceleration = 1000f;
		}
	}

	private void resetPoint() {
		mPoint[0].x = -5f;
		mPoint[1].x = -55f;
		mPoint[2].x = -105f;
		mPoint[3].x = -155f;
		mPoint[4].x = -205f;
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		// draw background
		canvas.drawRect(0, 0, mLayoutWidth, mLayoutHeight, mBackgroundPainter);

		// draw point
		for (int i = 0; i < mPoint.length; i++) {
			canvas.drawCircle(mPoint[i].x, mLayoutHeight / 2, 3f, mPointPainter);

		}
		mHandler.post(mUpdatePoint);
	}

	@Override
	public void setVisibility(int visibility) {
		super.setVisibility(visibility);
		if (visibility == View.INVISIBLE || visibility == View.GONE)
			dismiss();
	}

	public void dismiss() {
		mHandler.removeCallbacks(mUpdatePoint);
	}

}
